package com.baidu.location;

import android.content.Context;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationManager;
import android.os.Handler;
import android.os.Message;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

final class n
{
  private static File A;
  private static StringBuffer B;
  private static boolean C;
  private static int D;
  private static int E;
  private static long F;
  private static long G;
  private static long H;
  private static double I;
  private static double J;
  private static int K;
  private static int L;
  private static int M;
  private static int h = 0;
  private static String x = null;
  private static String z = "Temp_in.dat";
  private final long a = 1000L;
  private Context b;
  private LocationManager c = null;
  private Location d;
  private p e = null;
  private GpsStatus f;
  private o g = null;
  private long i = 0L;
  private long j = 0L;
  private boolean k = false;
  private Handler l = null;
  private boolean m = false;
  private String n = null;
  private boolean o = false;
  private long p = 0L;
  private final int q = 400;
  private boolean r = true;
  private boolean s = false;
  private List t = new ArrayList();
  private String u = null;
  private String v = null;
  private String w = null;
  private s y = null;

  static
  {
    A = new File(f.a, z);
    B = null;
    C = true;
    D = 0;
    E = 0;
    F = 0L;
    G = 0L;
    H = 0L;
    I = 0.0D;
    J = 0.0D;
    K = 0;
    L = 0;
    M = 0;
  }

  public n(Context paramContext, Handler paramHandler)
  {
    this.b = paramContext;
    this.l = paramHandler;
    try
    {
      if (ak.f != null)
      {
        this.y = new s(ak.f);
        return;
      }
      this.y = new s("NULL");
      return;
    }
    catch (Exception paramContext)
    {
      this.y = null;
    }
  }

  private static void A()
  {
    C = true;
    B = null;
    D = 0;
    E = 0;
    F = 0L;
    G = 0L;
    H = 0L;
    I = 0.0D;
    J = 0.0D;
    K = 0;
    L = 0;
    M = 0;
  }

  public static String a(Location paramLocation)
  {
    String str = c(paramLocation);
    paramLocation = str;
    if (str != null)
      paramLocation = str + "&g_tp=0";
    return paramLocation;
  }

  private void a(boolean paramBoolean)
  {
    this.o = paramBoolean;
    if ((paramBoolean) && (!(e())));
    do
    {
      do
        return;
      while (ak.b == paramBoolean);
      ak.b = paramBoolean;
    }
    while (!(ak.a));
    this.l.obtainMessage(53).sendToTarget();
  }

  public static boolean a(Location paramLocation1, Location paramLocation2, boolean paramBoolean)
  {
    float f2;
    if (paramLocation1 == paramLocation2);
    do
    {
      do
      {
        float f1;
        while (true)
        {
          return false;
          if ((paramLocation1 == null) || (paramLocation2 == null))
            return true;
          f1 = paramLocation2.getSpeed();
          if ((paramBoolean) && (ak.p == 3) && (f1 < 5.0F))
            return true;
          f2 = paramLocation2.distanceTo(paramLocation1);
          if (f1 <= ak.F)
            break;
          if (f2 > ak.H)
            return true;
        }
        if (f1 <= ak.E)
          break label88;
      }
      while (f2 <= ak.G);
      label88: return true;
    }
    while (f2 <= 5.0F);
    return true;
  }

  public static String b(Location paramLocation)
  {
    String str = c(paramLocation);
    paramLocation = str;
    if (str != null)
      paramLocation = str + x;
    return paramLocation;
  }

  private static boolean b(int paramInt1, int paramInt2, int paramInt3)
  {
    if ((paramInt1 < 0) || (paramInt1 > ak.U));
    do
      return false;
    while ((paramInt2 < 0) || (paramInt2 > paramInt1 + 1) || (paramInt3 <= 0) || (paramInt3 > paramInt1 + 1) || (paramInt3 > ak.U));
    return true;
  }

  private static String c(Location paramLocation)
  {
    double d1;
    float f1;
    int i1;
    if (paramLocation == null)
      return null;
    float f2 = (float)(paramLocation.getSpeed() * 3.6D);
    if (paramLocation.hasAccuracy())
    {
      f1 = paramLocation.getAccuracy();
      i1 = (int)f1;
      if (!(paramLocation.hasAltitude()))
        break label143;
      d1 = paramLocation.getAltitude();
    }
    while (true)
    {
      while (true)
      {
        return String.format("&ll=%.5f|%.5f&s=%.1f&d=%.1f&ll_r=%d&ll_n=%d&ll_h=%.2f&ll_t=%d", new Object[] { Double.valueOf(paramLocation.getLongitude()), Double.valueOf(paramLocation.getLatitude()), Float.valueOf(f2), Float.valueOf(paramLocation.getBearing()), Integer.valueOf(i1), Integer.valueOf(h), Double.valueOf(d1), Long.valueOf(paramLocation.getTime() / 1000L) });
        f1 = -1.0F;
      }
      label143: d1 = 555.0D;
    }
  }

  public static String f()
  {
    ak.e();
    if (A == null)
      return null;
    if (!(A.exists()))
      return null;
    try
    {
      RandomAccessFile localRandomAccessFile = new RandomAccessFile(A, "rw");
      localRandomAccessFile.seek(0L);
      int i3 = localRandomAccessFile.readInt();
      int i2 = localRandomAccessFile.readInt();
      int i1 = localRandomAccessFile.readInt();
      if (!(b(i3, i2, i1)))
      {
        localRandomAccessFile.close();
        z();
        return null;
      }
      ak.e();
      if ((i2 == 0) || (i2 == i1))
      {
        localRandomAccessFile.close();
        return null;
      }
      ak.e();
      long l1 = (i2 - 1) * 1024 + 12 + 0L;
      localRandomAccessFile.seek(l1);
      int i4 = localRandomAccessFile.readInt();
      Object localObject = new byte[i4];
      localRandomAccessFile.seek(l1 + 4L);
      i1 = 0;
      while (i1 < i4)
      {
        localObject[i1] = localRandomAccessFile.readByte();
        i1 += 1;
      }
      localObject = new String(localObject);
      if (i3 < ak.U);
      for (i1 = i2 + 1; ; i1 = 1)
      {
        localRandomAccessFile.seek(4L);
        localRandomAccessFile.writeInt(i1);
        localRandomAccessFile.close();
        return localObject;
        i1 = ak.U;
        if (i2 != i1)
          break;
      }
      i1 = i2 + 1;
    }
    catch (IOException localIOException)
    {
    }
    return ((String)null);
  }

  private static boolean z()
  {
    if (A.exists())
      A.delete();
    if (!(A.getParentFile().exists()))
      A.getParentFile().mkdirs();
    try
    {
      A.createNewFile();
      RandomAccessFile localRandomAccessFile = new RandomAccessFile(A, "rw");
      localRandomAccessFile.seek(0L);
      localRandomAccessFile.writeInt(0);
      localRandomAccessFile.writeInt(0);
      localRandomAccessFile.writeInt(1);
      localRandomAccessFile.close();
      A();
      return A.exists();
    }
    catch (IOException localIOException)
    {
    }
    return false;
  }

  public final void a()
  {
    if (this.m == true)
      return;
    try
    {
      this.c = ((LocationManager)this.b.getSystemService("location"));
      this.e = new p(this, 0);
      this.g = new o(this, 0);
      this.c.requestLocationUpdates("gps", 1000L, 5.0F, this.e);
      this.c.addGpsStatusListener(this.g);
      this.c.addNmeaListener(this.g);
      this.m = true;
      return;
    }
    catch (Exception localException)
    {
    }
  }

  public final void b()
  {
    if (!(this.m))
      return;
    if (this.c != null);
    try
    {
      if (this.e != null)
        this.c.removeUpdates(this.e);
      if (this.g != null)
      {
        this.c.removeGpsStatusListener(this.g);
        this.c.removeNmeaListener(this.g);
      }
      if (this.y != null)
        s.a();
      this.e = null;
      this.g = null;
      this.c = null;
      this.m = false;
      a(false);
      return;
    }
    catch (Exception localException)
    {
    }
  }

  public final String c()
  {
    float f1;
    String str = null;
    if (this.d != null)
    {
      str = "{\"result\":{\"time\":\"" + ak.a() + "\",\"error\":\"61\"},\"content\":{\"point\":{\"x\":\"%f\",\"y\":\"%f\"},\"radius\":\"%d\",\"d\":\"%f\",\"s\":\"%f\",\"n\":\"%d\"}}";
      if (!(this.d.hasAccuracy()))
        break label266;
      f1 = this.d.getAccuracy();
    }
    while (true)
    {
      int i1 = (int)f1;
      f1 = (float)(this.d.getSpeed() * 3.6D);
      double[] arrayOfDouble = Jni.a(this.d.getLongitude(), this.d.getLatitude(), "gps2gcj");
      if ((arrayOfDouble[0] <= 0.0D) && (arrayOfDouble[1] <= 0.0D))
      {
        arrayOfDouble[0] = this.d.getLongitude();
        arrayOfDouble[1] = this.d.getLatitude();
      }
      str = String.format(str, new Object[] { Double.valueOf(arrayOfDouble[0]), Double.valueOf(arrayOfDouble[1]), Integer.valueOf(i1), Float.valueOf(this.d.getBearing()), Float.valueOf(f1), Integer.valueOf(h) });
      new StringBuilder("wgs84: ").append(this.d.getLongitude()).append(" ").append(this.d.getLatitude()).append(" gcj02: ").append(arrayOfDouble[0]).append(" ").append(arrayOfDouble[1]);
      ak.e();
      return str;
      label266: f1 = 10.0F;
    }
  }

  public final Location d()
  {
    return this.d;
  }

  public final boolean e()
  {
    int i1;
    long l1;
    if ((this.d != null) && (this.d.getLatitude() != 0.0D) && (this.d.getLongitude() != 0.0D))
    {
      i1 = 1;
      if (i1 != 0)
        break label44;
    }
    do
    {
      do
      {
        while (true)
        {
          return false;
          i1 = 0;
        }
        label44: l1 = System.currentTimeMillis();
      }
      while ((this.k) && (l1 - this.j > 3000L));
      if (h >= 3)
        return true;
    }
    while (l1 - this.i >= 3000L);
    return true;
  }
}